<template>
  <!-- 放置一个弹层 用来编辑新增节点 -->
  <el-dialog :title="`${showText}权限点`" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" style="width:90%" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="showDialog = false">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addPermission, updatePermission, getPermissionDetail } from '@/api/permisson'
export default {
  name: 'AddPerm',
  data() {
    return {
      formData: {},
      showDialog: false,
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        code: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  methods: {
    async btnOK() {
      await this.$refs.perForm.validate()
      // 发送请求
      if (this.formData.id) {
        await updatePermission(this.formData)
        this.$message.success('更新成功')
      } else {
        await addPermission(this.formData)
        this.$message.success('添加成功')
      }
      this.showDialog = false
      this.$emit('updata')
    },
    btnCancel() {
      this.showDialog = false
      this.formData = {}
    },
    async getSingleInfo(id) {
      const { data } = await getPermissionDetail(id)
      this.formData = data
      this.showDialog = true
    }
  }
}
</script>

<style>

</style>
